package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShoppingCartMapper;

import com.sky.service.ISetmealService;
import com.sky.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-18
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private ISetmealService setmealService;


    @Transactional
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart cart = new ShoppingCart();
        BeanUtil.copyProperties(shoppingCartDTO, cart);

        //设置用户id
        cart.setUserId(BaseContext.getCurrentId());


        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getUserId() != null, ShoppingCart::getUserId, cart.getUserId())
                .eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                .orderByDesc(ShoppingCart::getCreateTime);
        //1.判断当前用户加入的购物车中的商品是否已经存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(wrapper);
        //shoppingCartList要么查不到要么只有一条数据
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //2.如果存在，则直接数量+1

            cart = shoppingCartList.get(0);//取出最新的记录
            cart.setNumber(cart.getNumber() + 1);
            this.updateById(cart);
        } else {
            //3.如果不存在，则直接加入(插入数据）购物车 ,数量就是1

            Long dishId = shoppingCartDTO.getDishId();
            //3.1 先判断是菜品还是套餐
            if (dishId != null) {
                //3.2 如果是菜品，则直接插入菜品数据（name，image，price）
                Dish dish = dishMapper.selectById(dishId);
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
            } else {
                //3.3 如果是套餐 则插入套餐数据（name，image，price）
                Setmeal setmeal = setmealService.getById(shoppingCartDTO.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }
//            则直接加入(插入数据）购物车 ,数量就是1
            cart.setNumber(1);
            shoppingCartMapper.insert(cart);


        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return shoppingCartMapper.selectList(wrapper);
    }
}
